﻿#include "FITKAcousticsAbstractSpectra.h"
#include "FITKAcousticsPhysics.h"
#include "FITKSpectraAlgorithm.h"
#include "FITKSpectraFrequencyGenerator.h"

namespace Acoustics
{
    FITKAcousticsAbstractSpectra::FITKAcousticsAbstractSpectra()
    {
        //创建数据
        _spectraAlg = new FITKSpectraAlgorithm(this);
    }

    FITKAcousticsAbstractSpectra::~FITKAcousticsAbstractSpectra()
    {
        //销毁数据
        if (_spectraAlg) delete _spectraAlg;
    }

    FITKAcousticsAbstractSpectra::AcousticsSpectraType FITKAcousticsAbstractSpectra::getSpectraType()
    {
        //频谱类型:无效值
        return None;
    }

    FITKGlobalFrequencyDomain::DomainType FITKAcousticsAbstractSpectra::getDomainType()
    {
        //频率类型
        return _domainType;
    }

    void FITKAcousticsAbstractSpectra::init()
    {
        //初始化数据
        FITKGlobalFrequencyDomain* freDomain = FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame()->getFrequencyDomain();
        if (freDomain == nullptr)return;
        _domainType = freDomain->getDomainType();
        switch (_domainType){
        case Acoustics::FITKGlobalFrequencyDomain::DTExact1OfnthOctave: _octaveNum = freDomain->getOctaveNum(); break;
        case Acoustics::FITKGlobalFrequencyDomain::DTConstantBandWidth: _bandWidth = freDomain->getBandWidth(); break;
        }
        QList<double> frequenceList = freDomain->getFrequenceList();
        //qDebug() << frequenceList;
        this->resetSpectraValues(frequenceList);
        initData();
    }

    void FITKAcousticsAbstractSpectra::init(double startValue, double endValue, FITKGlobalFrequencyDomain::DomainType type)
    {
        //初始化数据
        FITKSpectraFrequencyGenerator frequenceAlg;
        frequenceAlg.setDomainType(type);
        frequenceAlg.setFrequenceRange(startValue, endValue);
        _domainType = type;
        QList<double> frequenceList = frequenceAlg.getFrequencyList();
        //qDebug() << frequenceList;
        this->resetSpectraValues(frequenceList);
        initData();
    }

    void FITKAcousticsAbstractSpectra::init(double startValue, double endValue, FITKGlobalFrequencyDomain::DomainType type, int octaveNum)
    {
        //初始化数据
        FITKSpectraFrequencyGenerator frequenceAlg;
        frequenceAlg.setDomainType(type);
        frequenceAlg.setOctaveNum(octaveNum);
        _octaveNum = octaveNum;
        frequenceAlg.setFrequenceRange(startValue, endValue);
        _domainType = type;
        QList<double> frequenceList = frequenceAlg.getFrequencyList();
        //qDebug() << frequenceList;
        this->resetSpectraValues(frequenceList);
        initData();
    }

    void FITKAcousticsAbstractSpectra::init(double startValue, double endValue, FITKGlobalFrequencyDomain::DomainType type, double bandWidth)
    {
        //初始化数据
        FITKSpectraFrequencyGenerator frequenceAlg;
        frequenceAlg.setDomainType(type);
        frequenceAlg.setBandWidth(bandWidth);
        _bandWidth = bandWidth;
        frequenceAlg.setFrequenceRange(startValue, endValue);
        _domainType = type;
        QList<double> frequenceList = frequenceAlg.getFrequencyList();
        //qDebug() << frequenceList;
        this->resetSpectraValues(frequenceList);
        initData();
    }

    void FITKAcousticsAbstractSpectra::copy(FITKAcousticsAbstractSpectra* copyData)
    {
        if (copyData == nullptr)return;
        this->setDataObjectName(copyData->getDataObjectName());
        _notes = copyData->getNotes();
        _domainType = copyData->getDomainType();
        _spectraValues = copyData->getSpectraValue();
    }

    QList<SpectraValue> FITKAcousticsAbstractSpectra::getSpectraValue()
    {
        //获取频谱数据
        return _spectraValues;
    }

    void FITKAcousticsAbstractSpectra::setSpectraValue(const QList<SpectraValue> spectraData)
    {
        //设置频谱数据
        this->clearSpectraValues();
        _spectraValues = spectraData;
    }

    double FITKAcousticsAbstractSpectra::getStartFreq()
    {
        //获取起始频率
        if (_spectraValues.count() <= 0)return -1;
        return _spectraValues[0]._hz;
    }

    double FITKAcousticsAbstractSpectra::getEndFreq()
    {
        //获取终止频率
        if (_spectraValues.count() <= 0)return -1;
        return _spectraValues[_spectraValues.count() - 1]._hz;
    }

    FITKSpectraAlgorithm* FITKAcousticsAbstractSpectra::getSpectraAlg()
    {
        //获取频谱算法
        return _spectraAlg;
    }

    void FITKAcousticsAbstractSpectra::setSpectraValueAt(const int & index, const double & r /*= 1*/, const double & i /*= 0*/)
    {
        //设置实部与虚部
        if (index < 0 || index >= _spectraValues.size()) return;
        _spectraValues[index]._value.setReal(r);
        _spectraValues[index]._value.setImaginary(i);
    }

    int FITKAcousticsAbstractSpectra::getSpectraCount()
    {
        //获取频谱数据数量
        return _spectraValues.count();
    }

    SpectraValue FITKAcousticsAbstractSpectra::getSpectraValueAt(const int index) const
    {
        //获取频谱值
        if (index < 0 || index >= _spectraValues.size()) return SpectraValue();
        return _spectraValues[index];
    }

    double FITKAcousticsAbstractSpectra::getSpectraRealValue(const int index) const
    {
        //获取频谱值的实部值
        SpectraValue sv = this->getSpectraValueAt(index);
        return sv._value.getReal();
    }

    void FITKAcousticsAbstractSpectra::clearSpectraValues()
    {
        //清理频谱值
        _spectraValues.clear();
    }

    void FITKAcousticsAbstractSpectra::resetSpectraValues(const QList<double>& HzList)
    {
        //清空数据
        this->clearSpectraValues();
        //设置默认值
        SpectraValue v;
        v._value.setReal(1);
        //每个频率生成对应值
        for (double hz : HzList)
        {
            v._hz = hz;
            _spectraValues.append(v);
        }
    }

    bool FITKAcousticsAbstractSpectra::isRealValue()
    {
        return false;
    }

    void FITKAcousticsAbstractSpectra::setNotes(QString notes)
    {
        //设置注释
        _notes = notes;
    }

    QString FITKAcousticsAbstractSpectra::getNotes()
    {
        //获取注释
        return _notes;
    }

    QList<SpectraValue> FITKAcousticsAbstractSpectra::getRealSpectraValue()
    {
        return QList<SpectraValue>();
    }

    int FITKAcousticsAbstractSpectra::getOctaveNum()
    {
        return _octaveNum;
    }

    double FITKAcousticsAbstractSpectra::getBandWidth()
    {
        return _bandWidth;
    }
}


